package com.smxy.daydayup.util.osa.impl.local;

import com.smxy.daydayup.util.osa.OSAProvider;
import com.smxy.daydayup.util.osa.SelfHelpFileFetcher;
import com.smxy.daydayup.util.osa.mapper.ObjectStorageInfoMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * @Author: yusy
 * @Date: 2022/8/11
 */
@Configuration
@ConfigurationProperties(prefix = LocalDiskProperties.prefix)
public class LocalDiskProperties {
    private static final Logger log = LoggerFactory.getLogger(LocalDiskProperties.class);

    public static final String prefix = "component.osa.localdisk";
    String basePath;
    Long cacheMaxAge = 31536000L;

    public Long getCacheMaxAge() {
        return cacheMaxAge;
    }

    public void setCacheMaxAge(Long cacheMaxAge) {
        this.cacheMaxAge = cacheMaxAge;
    }

    public String getBasePath() {
        return basePath;
    }

    public void setBasePath(String basePath) {
        this.basePath = basePath.endsWith("/") ? basePath : (basePath + "/");
    }


    @Bean
    @ConditionalOnMissingBean(OSAProvider.class)
    @ConditionalOnProperty(prefix = LocalDiskProperties.prefix, value = {"basePath"})
    @Primary
    public OSAProvider localDiskOSAProvider(@Autowired LocalDiskProperties properties, @Autowired ObjectStorageInfoMapper objectStorageInfoMapper) {

        return new LocalDiskOSAProvider(properties, objectStorageInfoMapper);
    }

    @Bean
    @ConditionalOnMissingBean(SelfHelpFileFetcher.class)
    @ConditionalOnBean(name = "localDiskOSAProvider")
    public SelfHelpFileFetcher localSelfHelpFileFetcher(@Autowired OSAProvider localDiskOSAProvider) {
        log.warn("OSA启用了本地存储策略，注意在object_storage_info表给url字段加普通索引");
        return (SelfHelpFileFetcher)localDiskOSAProvider;
    }
}
